Type Audio

	Global soundResources:TList = New TList
	Global soundResourceMap:TMap = CreateMap()
	
	Function Init%()
	
		Local soundDir% = ReadDir("sound")
		
		If Not soundDir
		
			Console.ConsolePrint("Error: Unable to open sound directory")
			
			Return False
			
		EndIf
		
		Local fileName$ = NextFile$(soundDir)
		
		While fileName$ <> ""
			
			Local extension$ = Lower$(Right$(fileName$, 4))
			
			If extension$ = ".wav" Or extension$ = ".ogg"
			
				Local mySoundResource:SoundResource = New SoundResource
				
				mySoundResource.path$ = "sound\" + fileName$
				mySoundResource.sound = LoadSound(mySoundResource.path$)
				
				If mySoundResource.sound
				
					soundResources.AddLast(mySoundResource)
					MapInsert(soundResourceMap, mySoundResource.path$, mySoundResource)
					
				Else
				
					Console.ConsolePrint("Error loading sound resource '" + mySoundResource.path$ + "'")
					
				EndIf
				
			EndIf
			
			fileName$ = NextFile$(soundDir)
		
		Wend
		
		CloseDir(soundDir)
		
		Return True
		
	End Function

	Function Update%()
		
		Return True
			
	End Function
	
	Function Shutdown%()
		
		soundResources.Clear()
		ClearMap(soundResourceMap)
		
		Return True
		
	End Function
	
	Function GetSoundResource:SoundResource(path$)
	
		Return SoundResource(MapValueForKey(soundResourceMap, path$))
	
	End Function
	
End Type

Type SoundResource Extends Resource

	Field sound:TSound

End Type
